package com.idp.web.bp.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.base.BaseController;
import com.common.constant.SysMessage;
import com.common.model.JsonModel;
import com.common.model.PageModel;
import com.common.model.UploadModel;
import com.common.utils.UploadUtil;
import com.common.utils.ValidateUtil;
import com.idp.biz.bpchannel.entity.BpChannel;
import com.idp.biz.bpchannel.service.BpChannelService;

 /**
 * 热门频道controller
 * 
 * @author Stone
 *
 */
@Controller
@RequestMapping("/bpChannel/")
public class BpChannelController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(BpChannelController.class);

	@Autowired
	private BpChannelService bpChannelService;
	
	/**
	 * 初始化查询页面
	 * Stone 2019-06-29
	 * @return
	 */
	@RequestMapping(value="init",method=RequestMethod.GET)
	public String init(){
		logger.info("热门频道");
		return "bp/bpChannel/bpChannelList";
	}
	
	/**
	 * 分页查询
	 * Stone 2019-06-29
	 * @param bpChannel
	 * @param page
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.POST)
	@ResponseBody
	public PageModel<BpChannel> list(BpChannel bpChannel,PageModel<BpChannel> page){
		return bpChannelService.findByPage(bpChannel, page);
	}

	/**
	 * 新增修改页面初始化
	 * Stone 2019-06-29
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="bpChannel",method=RequestMethod.GET)
	public String bpChannel(String id,HttpServletRequest request){
		if(ValidateUtil.isNotEmpty(id)){
			BpChannel bpChannel = bpChannelService.getById(id);
			request.setAttribute("bpChannel", bpChannel);
		}
		return "bp/bpChannel/bpChannelEdit";
	}
	
	/**
	 * 详情
	 * Stone 2019-06-29
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="view",method=RequestMethod.GET)
	public String view(String id,HttpServletRequest request){
		if(ValidateUtil.isNotEmpty(id)){
			BpChannel bpChannel = bpChannelService.getById(id);
			request.setAttribute("bpChannel", bpChannel);
		}
		return "bp/bpChannel/bpChannelView";
	}
	
	/**
	 * 保存
	 * Stone 2019-06-29
	 * @param bpChannel
	 * @return
	 */
	@RequestMapping(value="save",method=RequestMethod.POST)
	@ResponseBody
	public JsonModel save(HttpServletRequest request) throws Exception {
		JsonModel json = new JsonModel();
		try {
			BpChannel bpChannel = new BpChannel();
			UploadModel model = new UploadModel(UploadUtil.getServerUploadBasePath(UploadUtil.PATH_IMAGES), UploadModel.IMAGES, bpChannel);
			UploadUtil.uploadFileSealedObject(request, model);
			if(model.isSuccess()){
				if(ValidateUtil.isNotEmpty(bpChannel.getId())){
					bpChannelService.update(bpChannel);
					json.setMessage(SysMessage.COMMON_UPDATE_SUCCESS);
				} else{
					bpChannelService.add(bpChannel);
					json.setMessage(SysMessage.COMMON_ADD_SUCCESS);
				}
			} else{
				json.setMessage(model.getMessage());
			}
		} catch (Exception e) {
			throw e;
		}
		return json;
	}
	
//	@RequestMapping(value="save",method=RequestMethod.POST)
//	@ResponseBody
//	public JsonModel save(BpChannel bpChannel, HttpServletRequest request){
//		JsonModel json = new JsonModel();
////		HttpSession session = request.getSession();
////		SysUser loginUser = (SysUser)session.getAttribute(SessionAttr.USER_LOGIN.getValue());
//		if(ValidateUtil.isNotEmpty(bpChannel.getId())){
////			bpChannel.setUpdateUser(loginUser.getName());
////			bpChannel.setUpdateDate(new Date());
//			bpChannelService.update(bpChannel);
//			json.setMessage(SysMessage.COMMON_UPDATE_SUCCESS);
//		} else{
////			bpChannel.setCreateUser(loginUser.getName());
//			bpChannel.setCreateTime(new Date());
//			bpChannelService.add(bpChannel);
//			json.setMessage(SysMessage.COMMON_ADD_SUCCESS);
//		}
//		return json;
//	}
	
	/**
	 * 删除
	 * Stone 2019-06-29
	 * @param id
	 * @return
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	public JsonModel delete(String ids){
		JsonModel json = new JsonModel();
		bpChannelService.delete(ids);
		json.setMessage(SysMessage.COMMON_DELETE_SUCCESS);
		return json;
	}
	
	/**
	 * 停启用
	 */
	@RequestMapping(value="changeStatus",method=RequestMethod.POST)
	@ResponseBody
	public JsonModel changeStatus(BpChannel bpChannel){
		JsonModel json = new JsonModel();
		bpChannelService.update(bpChannel);
		json.setMessage(SysMessage.COMMON_OPERATE_SUCCESS);
		return json;
	}
}
